package com.chima.action;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.chima.domain.Employees;
import com.chima.service.EmployeeService;
import com.opensymphony.xwork2.ActionSupport;
import flexjson.JSONSerializer;

public class LoginAction extends ActionSupport  {
	private String name;
	private String username;
	private String password;
	
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	private EmployeeService employeeService;
	
	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	private String message;
	private boolean success=false;
	
	public boolean isSuccess() {
		return success;
	}
	public void setSuccess(boolean success) {
		this.success = success;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	
	public String execute() throws Exception{
		System.out.println(username+"=="+password);
		String a="";
		Employees existEmployee =employeeService.findEmployeesBypwdAndName(username, password);
		
		// 判断登陆是否成功
		if(existEmployee == null){
			// 登陆失败
			a=INPUT;
//			JSONSerializer jsonSerializer = new JSONSerializer();
//	        String resultJson =jsonSerializer.serialize(success);
//			ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
//			ServletActionContext.getResponse().getWriter().print(resultJson);
		}else{
			a=SUCCESS;
			HttpSession  session = ServletActionContext.getRequest().getSession();
			session.setAttribute("existEmployee", existEmployee);
//			// 登陆成功 --- 将用户信息保存到Session
//			this.success=true;
//			JSONSerializer jsonSerializer = new JSONSerializer();
//	        String resultJson =jsonSerializer.serialize(success);
//			ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
//			ServletActionContext.getResponse().getWriter().print(resultJson);
//			
		}
		return a;
		
	}
	
}


